package UI;
import Service.StockService;
import Service.SecurityData;
import Service.SecurityLatestData;
import Service.Security;
import Service.LatestServiceClient;
import Service.HistoryServiceClient;
import facecat.topin.core.*;
import java.util.*;
import facecat.topin.service.*;
import facecat.topin.tab.*;
import facecat.topin.service.*;
import facecat.topin.grid.*;

public class MainFrame extends UIXmlEx implements FCPaintEventCallBack, FCListenerMessageCallBack, FCInvokeEventCallBack{
    public MainFrame()
    {
        m_mainFrame = this;
    }

    public static MainFrame m_mainFrame;

    public void changeTheme()
    {
        int nowStyle = MyColor.m_style;
        if (nowStyle == 0)
        {
            nowStyle = 1;
        }
        else
        {
            nowStyle = 0;
        }
        MyColor.setStyle(nowStyle);
    }

    public FCTabView m_tabFunc;

    public void loadXml(String xmlPath, FCView parent)
    {
        super.loadXml(xmlPath, null);
        m_tabFunc = (FCTabView)findView("tabFunc");
        m_tabFunc.addEvent(this, "onpaintborder", this);
        m_tabFunc.addEvent(this, "oninvoke", this);
        startConnect();
    }

    public void startConnect()
    {
        SecurityList securityList = (SecurityList)getNative().findView("gridStocks");
        securityList.firstSearchCode();
        searchCode("600000.SH");
    }

    public boolean callPaintEvent(String eventName, Object sender, FCPaint paint, FCRect clipRect, Object invoke)
    {
        FCView view = (FCView)((sender instanceof FCView) ? sender : null);
        paint.drawLine(FCColor.Border, 1, 0, 0, 0, view.getWidth(), 0);
        paint.drawLine(FCColor.Border, 1, 0, 0, 20, view.getWidth(), 20);
        return false;
    }

    public int m_requestID1 = -1;

    public int m_requestID2 = -1;

    public SecurityLatestData m_latestData;

    public static ArrayList<MyChart> m_charts = new ArrayList<MyChart>();

    public String m_lastCode = "";

    public void searchCode(String code)
    {
        if (m_requestID1 != -1)
        {
            DataCenter.m_latestServiceClient.removeListener(m_requestID1);
            DataCenter.m_latestServiceClient.unSubCodes(m_requestID1);
        }
        int oldReqID = m_requestID2;
        if (m_requestID2 != -1)
        {
            DataCenter.m_historyServiceClient.removeListener(m_requestID2);
        }
        m_requestID1 = FCClientService.getRequestID();
        m_requestID2 = FCClientService.getRequestID();
        for (int i = 0; i < m_charts.size(); i++)
        {
            MyChart myChart = m_charts.get(i);
            myChart.m_code = code;
            myChart.m_subscription = myChart.m_chart.getSubScription();
            myChart.m_chart.m_floatStrs.clear();
            if (myChart.m_cycle == 0)
            {
                myChart.m_chart.m_subComboBox.setVisible(false);
            }
            else
            {
                Security security = new Security();
                if (DataCenter.m_securityService.getSecurityByCode(myChart.m_code, security))
                {
                    if (security.m_type > 0)
                    {
                        myChart.m_chart.m_subComboBox.setVisible(true);
                    }
                    else
                    {
                        myChart.m_chart.m_subComboBox.setVisible(false);
                    }
                }
                else
                {
                    myChart.m_chart.m_subComboBox.setVisible(false);
                }
            }
        }
        m_latestData = null;
        DataCenter.m_latestServiceClient.addListener(m_requestID1, this, null);
        DataCenter.m_historyServiceClient.addListener(m_requestID2, this, null);
        DataCenter.m_latestServiceClient.subCodes(m_requestID1, code);
        DataCenter.m_historyServiceClient.reqData2(m_requestID2, code, oldReqID);
        m_lastCode = code;
    }

    public void callListenerMessageEvent(Object sender, FCMessage message, Object invoke)
    {
        m_tabFunc.invoke(message);
    }

    public void bindData(FCMessage message)
    {
        if (message.m_functionID == HistoryServiceClient.FUNCTIONID_GETDATA2)
        {
            if (message.m_requestID == m_requestID2)
            {
                String code = "";
                ArrayList<SecurityData> dayDatas = new ArrayList<SecurityData>();
                ArrayList<SecurityData> minuteDatas = new ArrayList<SecurityData>();
                ArrayList<SecurityData> trendDatas = new ArrayList<SecurityData>();
                RefObject<String> Refcode = new RefObject<String>(code);
                HistoryServiceClient.getDatas2(Refcode, dayDatas, minuteDatas, trendDatas, message.m_body, message.m_bodyLength);
                code = Refcode.argvalue;
                boolean hasAfter = false;
                for (int i = 0; i < m_charts.size(); i++)
                {
                    MyChart myChart = m_charts.get(i);
                    if (myChart.m_cycle > 0)
                    {
                        if (myChart.m_subscription == 2)
                        {
                            hasAfter = true;
                        }
                    }
                }
                ArrayList<SecurityData> beforeDatas = new ArrayList<SecurityData>();
                ArrayList<SecurityData> afterDatas = new ArrayList<SecurityData>();
                for (int i = 0; i < dayDatas.size(); i++)
                {
                    SecurityData data = dayDatas.get(i);
                    SecurityData fData = new SecurityData();
                    fData.copy(data);
                    beforeDatas.add(fData);
                    if (hasAfter)
                    {
                        SecurityData bData = new SecurityData();
                        bData.copy(data);
                        afterDatas.add(bData);
                    }
                }
                ArrayList<SecurityData> beforeDatas2 = new ArrayList<SecurityData>();
                ArrayList<SecurityData> afterDatas2 = new ArrayList<SecurityData>();
                for (int i = 0; i < minuteDatas.size(); i++)
                {
                    SecurityData data = minuteDatas.get(i);
                    SecurityData fData = new SecurityData();
                    fData.copy(data);
                    beforeDatas2.add(fData);
                    if (hasAfter)
                    {
                        SecurityData bData = new SecurityData();
                        bData.copy(data);
                        afterDatas2.add(bData);
                    }
                }
                StockService.ConvertXDR(code, 1, beforeDatas);
                if (hasAfter)
                {
                    StockService.ConvertXDR(code, 2, afterDatas);
                }

                StockService.ConvertXDR(code, 1, beforeDatas2);
                if (hasAfter)
                {
                    StockService.ConvertXDR(code, 2, afterDatas2);
                }
                for (int i = 0; i < m_charts.size(); i++)
                {
                    MyChart myChart = m_charts.get(i);
                    if (myChart.m_cycle == 0)
                    {
                        myChart.bindHistoryDatas(myChart.m_code, myChart.m_cycle, trendDatas, false);
                    }
                    else if (myChart.m_cycle > 0 && myChart.m_cycle < 1440)
                    {
                        ArrayList<SecurityData> sDatas = null;
                        if (myChart.m_subscription == 1)
                        {
                            sDatas = beforeDatas2;
                        }
                        else if (myChart.m_subscription == 2)
                        {
                            sDatas = afterDatas2;
                        }
                        else
                        {
                            sDatas = minuteDatas;
                        }
                        if (myChart.m_cycle > 1)
                        {
                            ArrayList<SecurityData> newDatas = new ArrayList<SecurityData>();
                            StockService.multiMinuteSecurityDatas(newDatas, sDatas, myChart.m_cycle);
                            myChart.bindHistoryDatas(myChart.m_code, myChart.m_cycle, newDatas, false);
                        }
                        else
                        {
                            myChart.bindHistoryDatas(myChart.m_code, myChart.m_cycle, sDatas, false);
                        }
                    }
                    else if (myChart.m_cycle >= 1440)
                    {
                        ArrayList<SecurityData> sDatas = null;
                        if (myChart.m_subscription == 1)
                        {
                            sDatas = beforeDatas;
                        }
                        else if (myChart.m_subscription == 2)
                        {
                            sDatas = afterDatas;
                        }
                        else
                        {
                            sDatas = dayDatas;
                        }
                        if (myChart.m_cycle == 10080)
                        {
                            ArrayList<SecurityData> newDatas = new ArrayList<SecurityData>();
                            StockService.getHistoryWeekDatas(newDatas, sDatas);
                            myChart.bindHistoryDatas(myChart.m_code, myChart.m_cycle, newDatas, false);
                        }
                        else if (myChart.m_cycle == 43200)
                        {
                            ArrayList<SecurityData> newDatas = new ArrayList<SecurityData>();
                            StockService.getHistoryMonthDatas(newDatas, sDatas);
                            myChart.bindHistoryDatas(myChart.m_code, myChart.m_cycle, newDatas, false);
                        }
                        else if (myChart.m_cycle == 129600)
                        {
                            ArrayList<SecurityData> newDatas = new ArrayList<SecurityData>();
                            StockService.getHistorySeasonDatas(newDatas, sDatas);
                            myChart.bindHistoryDatas(myChart.m_code, myChart.m_cycle, newDatas, false);
                        }
                        else if (myChart.m_cycle == 259200)
                        {
                            ArrayList<SecurityData> newDatas = new ArrayList<SecurityData>();
                            StockService.getHistoryHalfYearDatas(newDatas, sDatas);
                            myChart.bindHistoryDatas(myChart.m_code, myChart.m_cycle, newDatas, false);
                        }
                        else if (myChart.m_cycle == 518400)
                        {
                            ArrayList<SecurityData> newDatas = new ArrayList<SecurityData>();
                            StockService.getHistoryYearDatas(newDatas, sDatas);
                            myChart.bindHistoryDatas(myChart.m_code, myChart.m_cycle, newDatas, false);
                        }
                        else
                        {
                            myChart.bindHistoryDatas(myChart.m_code, myChart.m_cycle, sDatas, false);
                        }
                    }
                }
                dayDatas.clear();
                minuteDatas.clear();
                trendDatas.clear();
                beforeDatas.clear();
                afterDatas.clear();
                beforeDatas2.clear();
                afterDatas2.clear();

                getNative().invalidate();
            }
        }
        else if (message.m_functionID == LatestServiceClient.FUNCTION_NEWDATA)
        {
            if (message.m_requestID == m_requestID1)
            {
                SecurityLatestData latestData = LatestServiceClient.getLatestData(message.m_body, message.m_bodyLength);
//                LatestDiv latestDiv = findView("divLatest") as LatestDiv;
                FCView parent = findView("divLatest");
                LatestDiv latestDiv = (LatestDiv)((parent instanceof LatestDiv) ? parent : null);
                
                
                latestData.m_requestID = m_requestID1;
                if (m_latestData != null && m_latestData.m_requestID == m_requestID1)
                {
                    for (int i = 0; i < m_charts.size(); i++)
                    {
                        MyChart myChart = m_charts.get(i);
                        if (myChart.m_chart.m_datas.size() > 0)
                        {
                            myChart.newData(latestData, latestData.m_requestID, true);
                        }
                    }
                }
                m_latestData = latestData;
                latestDiv.bindData(latestData, true);
            }
        }
    }

    public void callInvokeEvent(String eventName, Object sender, Object args, Object invoke)
    {
        if (args != null)
        {
            
            FCMessage message = (FCMessage)((args instanceof FCMessage) ? args : null);
            bindData(message);
        }
    }
}

